package com.chinaunicom.order.trigger.http;

import com.chinaunicom.common.result.JsonResult;
import com.chinaunicom.order.domain.model.entity.OrderItemEntity;
import com.chinaunicom.order.domain.model.entity.PayOrderEntity;
import com.chinaunicom.order.domain.model.entity.ShopCartEntity;
import com.chinaunicom.order.domain.service.IOrderService;
import com.chinaunicom.order.trigger.http.vo.ShopCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author guomenghuai
 * @date 2024/1/10
 * @Description
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/createOrder")
    public JsonResult<PayOrderEntity> createOrder(@RequestBody ShopCartVO shopCartVO) {
        List<OrderItemEntity> orderItems = shopCartVO.getOrderItemList().stream().map(item -> {
            OrderItemEntity orderItem = OrderItemEntity.builder()
                    .skuCode(item.getSkuCode())
                    .quantity(item.getQuantity())
                    .build();
            return orderItem;
        }).collect(Collectors.toList());
        ShopCartEntity shopCartEntity = ShopCartEntity.builder()
                .userId(shopCartVO.getUserId())
                .sellerId(shopCartVO.getSellerId())
                .orderItemList(orderItems)
                .build();
        return JsonResult.success(orderService.createOrder(shopCartEntity));
    }
}
